//
//  SettingStore.swift
//  NaiCha
//
//  Created by 唐安坤 on 2024/9/13.
//

import Foundation

class SettingStore: ObservableObject {
    init() {
        let s =  UserDefaults.standard.value(forKey: "SettingStore_selection") as? Int ?? 0
        self.selection = SelectionEnum(rawValue: s)
        self.gap = UserDefaults.standard.value(forKey: "SettingStore_gap") as? Int ?? 15
        self.toogle = UserDefaults.standard.value(forKey: "SettingStore_toogle") as? Bool ?? false
    }
    @Published var selection: SelectionEnum{
        willSet{
            print("---------",newValue)
        }
        didSet{
            UserDefaults.standard.setValue(selection.rawValue, forKey: "SettingStore_selection")
        }
    }
    @Published var gap:Int{
        didSet{
            UserDefaults.standard.setValue(gap, forKey: "SettingStore_gap")
        }
    }
    @Published var toogle: Bool{
        didSet{
            UserDefaults.standard.setValue(toogle, forKey: "SettingStore_toogle")
        }
    }
}

enum SelectionEnum: Int, CaseIterable {
    case showAll = 0
    case onlyPrice = 1
    case showNull = 2
    
    init(rawValue: Int) {
        switch rawValue{
        case 0:
            self = .showAll
        case 1:
            self = .onlyPrice
        case 2:
            self = .showNull
        default:
            self = .showAll
        }
    }
    
    var text:String{
        switch self {
        case .showAll:
            return "显示全部"
        case .onlyPrice:
            return "只显示价格"
        case .showNull:
            return "全部不显示"
        }
    }
    
}
